<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: [ JRK丶Admin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020/6/26 0026
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------

namespace app\admin\controller;


use app\admin\model\AdminUser;
use app\admin\validate\CheckAdminUser;
use app\common\controller\AdminBaseController;
use app\common\model\personnel\PersonnelOrder;
use app\common\model\personnel\PersonnelOrderChild;
use app\common\model\personnel\PersonnelScholar;
use app\common\model\personnel\PersonnelStudent;
use app\common\model\personnel\PersonnelType;
use app\common\service\GoogleAuthenticator;
use think\Exception;

class Order extends AdminBaseController
{
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->model = new PersonnelOrder();
    }


    /**
     * @return mixed|string|\think\response\Json
     * @throws \think\db\exception\DbException
     * @author: Hhy <jackhhy520@qq.com>
     * @date: 2020/6/30 0030
     * @describe:添加编辑
     */
    public function add()
    {
        if (IS_AJAX) {
            $data = $this->request->post();
            $submitData = [];
            $price = 0;
            foreach ($data['course_data'] as $key => $value){
                $submitData[] = [
                    'course_id' => $value['id'],
                    'order_id' => null,
                    'price' => $value['price']
                ];
                $price += $value['price'];
            }
            $data = [
                'student_id' => $data['student_data'][0]['id'],
                'price' => $price,
                'pay_status' => 1,
                'pay_way' => 3,
                'ticket' => 0,
                'verify_status' => 1
            ];
            $result =  $this->model->doAll([$data], true);

            $order_id = $result[0]->getAttr('id');
            foreach ($submitData as $key => &$value){
                $value['order_id'] = $order_id;
            }
            (new PersonnelOrderChild)->saveAll($submitData);

        }

        $id = $this->request->param("id/d");//父id
        $info = $this->model->find($id);

        $this->assign(compact("info", "id"));
        return $this->fetch();
    }


    public function show()
    {
        $id = $this->request->param("id/d");//父id
        $info = $this->model->with(['CourseChild','CourseStudent'])->find($id);

        $this->assign(compact("info", "id"));
        return $this->fetch();
    }

}